/**
 * 
 */
package codec;

import util.Log;
import util.Transformer;

/**
 * @author nquangtrung
 *
 */
public class JPEGQuantizationTable {
	public static final String TAG = "JPEGQuantizationTable";
	public static final byte[][] LUMINANCE 
     /*= {{16, 11, 10, 16, 24,  40,  51,  61  }, 
		{12, 12, 14, 19, 26,  58,  60,  55  }, 
		{14, 13, 16, 24, 40,  57,  69,  56  },
		{14, 17, 22, 29, 51,  87,  80,  62  },
		{18, 22, 37, 56, 68,  109, 103, 77  },
		{24, 35, 55, 64, 81,  104, 113, 92  },
		{49, 64, 78, 87, 103, 121, 120, 101 },
		{72, 92, 95, 98, 112, 100, 103, 99  }};*/
	 = {{16, 12, 14, 14, 18,  24,  49,  72  }, 
		{11, 12, 13, 17, 22,  35,  64,  92  }, 
		{10, 14, 16, 22, 37,  55,  78,  95  },
		{16, 19, 24, 29, 56,  64,  87,  98  },
		{24, 26, 40, 51, 68,  81, 103,  112 },
		{40, 58, 57, 87, 109, 104, 121, 100 },
		{51, 60, 69, 80, 103, 113, 120, 103 },
		{61, 55, 56, 62, 77,  92,  101, 99  }};
	public static final byte[][] CHROMINANCE 
	 = {{17, 18, 24, 47, 99, 99, 99, 99 }, 
		{18, 21, 26, 66, 99, 99, 99, 99 }, 
		{24, 26, 56, 99, 99, 99, 99, 99 },
		{47, 66, 99, 99, 99, 99, 99, 99 },
		{99, 99, 99, 99, 99, 99, 99, 99 },
		{99, 99, 99, 99, 99, 99, 99, 99 },
		{99, 99, 99, 99, 99, 99, 99, 99 },
		{99, 99, 99, 99, 99, 99, 99, 99 }};

	/**
	 * 
	 */
	public JPEGQuantizationTable() {
		// TODO Auto-generated constructor stub
	}
	
	public static byte[][] getQuantizationTable(byte[][] table, double quality) {
		byte[][] q = new byte[8][8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				q[i][j] = (byte) (table[i][j] * quality);
			}
			// System.out.println();
		}
		
		return q;
	}
	public static int fitByte(byte[] b, int offset, byte[][] table, int width, int height, double quality) {
		Transformer t = new Transformer();
		byte[][] q = getQuantizationTable(table, quality);
		byte[] bx = t.zigzag(q, width, height);
		Log.debugArray(TAG, "Before", q, 8, 8);
		Log.debugArray(TAG, "Quantization Table Zigzag", bx, 9);
		for (int i = offset; i < offset + width * height; i++) {
			b[i] = bx[i - offset];
		}
		return width * height;
	}
	
	public static int fitByte(byte[] b, int offset, byte[][] table, int width, int height) {
		return fitByte(b, offset, table, width, height, 1);
	}

}
